<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
<h1>getAjax</h1>
<form action="" id="aForm">

  <input placeholder="请输入id" id="ipt" type="text">
  <button id="btn">点击查询数据</button>

</form>
<script>
  const btn = document.querySelector('button')
  const aForm = document.querySelector('form')
  const ipt = document.querySelector('#ipt')
  //建立ajax
  aForm.onsubmit = function () {

    const userId = ipt.value

    const xhr = new XMLHttpRequest()
    //添加时间戳
    xhr.open('GET',`/user?userId=${userId}&${Date.now()}`,true)
    xhr.send()
    xhr.onreadystatechange = function () {

      if (xhr.readyState===4 && xhr.status>=200&&xhr.status<=299){
        console.log(xhr.responseText)
      }

    }

    //防止默认事件
    return false
  }
</script>
</body>
</html>